<?php
/**
 * ShopCartController.class.php
 * 购物车控制器
 *
 * @author    <王中艺>
 * @date    2017-02-06
 */

namespace Wechat\Controller;

class ShopCartController extends BaseController {

    /**
     * 发起支付
     * @param   $goods_type 商品类型
     * @param   $goods_ids 商品ID(array)
     * @param   $coupon_code 优惠码
     * @param   $score 幸福豆
     * www.xinletu.zy.com/server/wechat.php/shopcart/createpay?goods_type=1&goods_ids[]=20&goods_ids[]=21&goods_ids[]=22&coupon_code=E7A21_53DF6ABA6
     */
    function createPay(){
        $this->unEmptyParam(array('goods_type', 'goods_ids'));

        //购物车样品结算
        $goodsType  = $this->iInfo['goods_type'];
        $goodsIds   = $this->iInfo['goods_ids'];
        $couponCode = $this->iInfo['coupon_code'];

        $userId     = C('User.id');
        $amount     = 0;
        switch($goodsType){
            case 1:
                //已购买课程处理
                $hasBuy     = M('t_user_course', null)
                    ->where('user_id = %d and course_id in (%s)', $userId, implode(',', $goodsIds))
                    ->select();
                if($hasBuy)
                    $this->outOInfo('购物车中存在非法数据,请清空后重试!', 0);

                $goods  = D('Course')->batchGetCourses($goodsIds, 1, 1);
                foreach($goods as $course){
                    $amount     += $course['price'];
                }
                
                $remark     = json_encode(array('goods_name' => count($goods).'节课程', 'action' => '购买'));
                $goodsTitle     = '正心源课程';
            break;
        }
        if(count($goods) != count($goodsIds))
            $this->outOInfo('购物车中存在非法数据,请清空后重试!', 0);

        //检测优惠码
        if($couponCode){
            $codeInfo   = D('Coupon')->checkCouponCode($couponCode, $userId, $amount);
            if($codeInfo === false){
                $this->outOInfo(D('Coupon')->errMsg, 0);
            }
            $amount     -= $codeInfo['amount'];
        }

        //添加购物车信息
        $shopCartId     = D('ShopCart')->addCartInfo($userId, $goodsType, $goodsIds, $amount);
        if($shopCartId === false)
            $this->outOInfo('购物车信息添加失败!', 0);

        //添加支付信息
        $goodsType  = 4;
        $payCate    = 1;
        $codeId     = $codeInfo ? $codeInfo['id'] : 0;
        $moneyInfo  = D('Money')->createPay($userId, $goodsType, $shopCartId, $amount, $remark, $payCate, $codeId);
        
        //生成JSsdk支付信息
        $wxPay  = new \Wechat\Util\WxPay();
        $payInfo    = $wxPay->createJsPay(
            $moneyInfo['id'],
            $goodsTitle,
            $moneyInfo['trade_no'],
            $moneyInfo['amount'] * 100,
            C('user.open_id')
        );
        if(!$payInfo)
            $this->outOInfo('JSsdk支付信息生成失败!', 0);

        $this->outOInfo($payInfo);
    }

    function debug(){
        $result     = D('Coupon')->useCouponCode('E7A21_53DF6ABA6', C('user.id'), 300);
        dump($result);
        dump(D('Coupon')->errMsg);
    }
}
